package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.SysPtExpVideoAddBo;
import com.ruoyi.system.bo.SysPtExpVideoQueryBo;
import com.ruoyi.system.bo.SysPtExpVideoEditBo;
import com.ruoyi.system.domain.SysPtExpVideo;
import com.ruoyi.system.mapper.SysPtExpVideoMapper;
import com.ruoyi.system.vo.SysPtExpVideoVo;
import com.ruoyi.system.service.ISysPtExpVideoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 实验视频管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-28
 */
@Service
public class SysPtExpVideoServiceImpl extends ServiceImpl<SysPtExpVideoMapper, SysPtExpVideo> implements ISysPtExpVideoService {

    @Override
    public SysPtExpVideoVo queryById(Long videoId){
        return getVoById(videoId, SysPtExpVideoVo.class);
    }

    @Override
    public TableDataInfo<SysPtExpVideoVo> queryPageList(SysPtExpVideoQueryBo bo) {
        PagePlus<SysPtExpVideo, SysPtExpVideoVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysPtExpVideoVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysPtExpVideoVo> queryList(SysPtExpVideoQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysPtExpVideoVo.class);
    }

    private LambdaQueryWrapper<SysPtExpVideo> buildQueryWrapper(SysPtExpVideoQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPtExpVideo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExpId() != null, SysPtExpVideo::getExpId, bo.getExpId());
        lqw.eq(StrUtil.isNotBlank(bo.getTitle()), SysPtExpVideo::getTitle, bo.getTitle());
        lqw.eq(StrUtil.isNotBlank(bo.getUrl()), SysPtExpVideo::getUrl, bo.getUrl());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysPtExpVideoAddBo bo) {
        SysPtExpVideo add = BeanUtil.toBean(bo, SysPtExpVideo.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysPtExpVideoEditBo bo) {
        SysPtExpVideo update = BeanUtil.toBean(bo, SysPtExpVideo.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysPtExpVideo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
